package com.gack.business.service;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import com.gack.business.model.StoreUniteInfo;
import com.gack.business.repository.StoreUniteInfoRepository;
import com.gack.helper.common.abstractobj.Result;

/**
 * 
* @ClassName: StoreUniteInfoService 
* @Description: TODO(这里用一句话描述这个类的作用) 
* @author (ZhangXinYu)  
* @date 2018年6月7日 下午5:27:26 
* @version V1.0
 */
@Service
public class StoreUniteInfoService implements StoreUniteInfoServiceInterface{

	@Autowired
	private StoreUniteInfoRepository storeUniteInfoRepository;
	@Autowired
	private BackLogServiceInterface backLogService;
	
	/**
	 * 添加或修改门店统一信息
	 */
	@Override
	public Result addStoreUniteInfo(String userid,String key, String value) {
		Result result = new Result();
		Integer count = storeUniteInfoRepository.findUniteInfoKeyCount(key);
		if(count > 0){
			result.setKey("error_duplicateUniteinfo");
			result.setMsg("门店统一信息字段已存在，请勿重复添加");
			return result;
		}
		StoreUniteInfo storeUniteInfo = new StoreUniteInfo();
		storeUniteInfo.setUniteKey(key);
		storeUniteInfo.setUniteValue(value);
		storeUniteInfo = storeUniteInfoRepository.save(storeUniteInfo);
		backLogService.addBackLog(userid, "添加门店统一信息");
		result.setKey("success");
		result.setValue(storeUniteInfo);
		result.setMsg("添加门店统一信息成功");
		return result;
	}
	
	/**
	 * 修改门店统一信息
	 */
	@Override
	public Result updateStoreUniteInfo(String userid,String id,String value){
		Result result = new Result();
		StoreUniteInfo storeUniteInfo = storeUniteInfoRepository.findOne(id);
		if(storeUniteInfo == null){
			result.setKey("error_noneInfo");
			result.setMsg("统一门店信息不存在，修改操作失败");
			return result;
		}
		storeUniteInfo.setUniteValue(value);
		storeUniteInfo = storeUniteInfoRepository.save(storeUniteInfo);
		backLogService.addBackLog(userid, "修改门店统一信息");
		result.setKey("success");
		result.setValue(storeUniteInfo);
		result.setMsg("修改门店统一信息成功");
		return result;
	}

	/**
	 * 获取所有门店统一信息
	 */
	@Override
	public List<StoreUniteInfo> getStoreUniteInfos() {
		List<StoreUniteInfo> storeUniteInfos = null;
		storeUniteInfos = storeUniteInfoRepository.findAll();
		return storeUniteInfos;
	}
	
	/**
	 * 分页获取所有门店统一信息
	 */
	@Override
	public Page<StoreUniteInfo> getStoreUniteInfos(Integer currentPage,Integer pageSize){
		Page<StoreUniteInfo> storeUniteInfos = null;
		Pageable pageable = new PageRequest(currentPage, pageSize);
		storeUniteInfos = storeUniteInfoRepository.findAll(pageable);
		return storeUniteInfos;
	}

	/**
	 * 查询指定统一门店信息字段
	 */
	@Override
	public Result getStoreUniteInfoByKey(String key) {
		Result result = new Result();
		StoreUniteInfo storeUniteInfo = storeUniteInfoRepository.findUniteInfoByKey(key);
		if(storeUniteInfo == null){
			result.setKey("error_noneUniteInfo");
			result.setMsg("门店统一信息不存在");
			return result;
		}
		result.setKey("success");
		result.setValue(storeUniteInfo);
		result.setMsg("门店统一信息查询成功");
		return result;
	}
	
	/**
	 * 查询指定门店统一信息字段
	 */
	@Override
	public Result getStoreUniteInfo(String id){
		Result result = new Result();
		StoreUniteInfo storeUniteInfo = storeUniteInfoRepository.findOne(id);
		if(storeUniteInfo == null){
			result.setKey("error_noneUniteInfo");
			result.setMsg("统一门店信息不存在");
			return result;
		}
		result.setKey("success");
		result.setValue(storeUniteInfo);
		result.setMsg("统一门店信息查询成功");
		return result;
	}

	/**
	 * 删除指定门店统一信息
	 */
	@Override
	public Result dealStoreUniteInfo(String userid,String infoid) {
		Result result = new Result();
		Integer dealCount = storeUniteInfoRepository.dealUniteInfoById(infoid);
		if(dealCount == 0){
			result.setKey("error_dealError");
			result.setMsg("删除指定门店统一信息失败");
			return result;
		}
		backLogService.addBackLog(userid, "删除门店统一信息");
		result.setKey("success");
		result.setMsg("删除指定门店统一信息成功");
		return result;
	}

	/**
	 * 通过指定字段删除门店统一信息
	 */
	@Override
	public Result dealStoreUniteInfoByKey(String userid,String key) {
		Result result = new Result();
		Integer dealCount = storeUniteInfoRepository.dealUniteInfoByKey(key);
		if(dealCount == 0){
			result.setKey("error_dealError");
			result.setMsg("删除指定门店统一信息失败");
			return result;
		}
		backLogService.addBackLog(userid, "通过key值删除门店统一信息");
		result.setKey("success");
		result.setMsg("删除指定门店统一信息成功");
		return result;
	}

}
